/**
 * Created by WuJianFeng
 * Date: 11-7-19
 * Time: 上午10:37
 */
/****************************document ready start*******************************/
$(document).ready(function() {
    $("#x-toolbar-center").replaceWith($("#pkg-top-toolbar"));
});
/****************************document ready end*******************************/
var waiting = false;
var isProcessing = false;

var install = function() {
    var probe="";
    if ($("#pkgFile").val() == null || $("#pkgFile").val() == '') {
        $.ui.messageBox.show({title:l("package_dialog_title_install"),msg:l("package_alert_upload_null"),buttons:$.ui.messageBox.OK});
    } else {
        $.ui.messageBox.show({title:l("global_text_confirm"),msg:l("package_confirm_install"),buttons:$.ui.messageBox.OKCANCEL, callback: function(result) {
            if ($.ui.messageBox.OK[result]) {
                var isActive = $("#isActive").is(":checked");

                overLay(l("package_dialog_title_install"));
                 var file = "pkgFile";
                 $.ajaxFileUpload({
                        secureuri:false,

                        type: 'POST',
                        fileElementId: file,
                        url: '/probe_packages/install?isActive='+isActive+'&file='+file,
                        complete: function(response) {
                            isProcessing = false;
                            if(waiting){
                                $("#task-startTip").remove();
                            }
                            var msg;
                            if(response!=null&&response.responseText!=null){
                              msg = "<br/>" + response.responseText.toString();
                              probe=msg.substring(0,msg.indexOf(' '));}
                            else msg=l("net_error");
                            $("body").data("dialog").append(msg);
                            $("body").data("dialog").addButton({text:l("package_dialog_close"),handler:'window.location.href = window.location.href'});
                        }
                });
            }
        }});

    }
};

var validateFile = function(form) {
    var maxFileSize = 30;  <!-- 文件大小100M -->
    var location=form.pkgFile.value;
    var point = location.lastIndexOf(".");
    var type = location.substr(point);
    if(type==".jar"){
       var file = document.getElementById("pkgFile").value;
       var getAXO = new ActiveXObject("Scripting.FileSystemObject");
       var getF = getAXO.GetFile(file);
        if(getF>maxFileSize*1024*1024)  {
            alert("文件太大！");
            return false;
        }else{
            return true;
        }
    }
    else{
        alert("文件格式错误");
        return false;
    }

};


var unInstall = function() {
    $.ui.messageBox.show({title:l("global_text_confirm"),msg:l("package_confirm_uninstall"),buttons:$.ui.messageBox.OKCANCEL, callback: function(result) {
            if ($.ui.messageBox.OK[result]) {
                overLay(l("package_dialog_title_uninstall"));
                var probePackage = $("#probePackage").val();
                ajaxOverLayForProbe("/probe_packages/"+probePackage+"/unInstall","POST","",1,"",[{text:l("package_dialog_close"),handler:function(){
                    window.location.href = '/probe_packages';
                }
                }]);
            }
    }});
};

var Active = function() {
    overLays(l("package_dialog_title_engine"), function(engines,probes){
        overLay(l("package_dialog_title_active"));
        for(var j = 0;j<engines.length;j++){
            ajaxOverLayForProbe("/probe_packages/"+engines[j]+"_"+probes + "/activate","POST", "",engines.length ,"",
                      [{text:l("package_dialog_close"),handler:'window.location.href = window.location.href'}]);
        }
        });
        var probes  = $("#probePackage").val().replace("/","!");
        ajaxOverLayForProbe("/probe_packages/engines?probes="+probes+"&action=1", "GET","",1,"",
                    [{text:l("package_dialog_ok"),handler:function() {
                        var enginesId = $("#engineList_id").attr("value");
                        var engines = $("#"+enginesId).data("guilist").getCheckedValues();
                        if(engines.length == 0){
                            $.ui.messageBox.show({title:l("package_dialog_title"),msg:l("package_dialog_engines_null"),buttons:$.ui.messageBox.OK,callback:function(){
                                if($.ui.messageBox.OK || $.ui.messageBox.CANCEL) {
                                    $(".ui-dialog").addClass('ui-overlay-element');
                                    $(".ui-overlay").css('z-index','10');
                                }
                            }});
                            $(".ui-overlay-element").removeClass("ui-overlay-element");
                            $(".ui-overlay").css('z-index','0');
                        }else {
                            return [engines,probes]
                        }
                    }
                    },
                     {text:l("package_dialog_close"),handler:'window.location.href = window.location.href'}
                    ]);
        return false;
};

var uploadLabel = function() {
    $("#upload-label").attr("value",$("#pkgFile").attr("value"));
};

var overLays = function(title, callback) {
    var win = $("body").dialog({"title":title,"closable":true,"modal":true,width:500,height:250}).data("dialog");
    win.show(callback);
};

var showPackageContent = function() {
    if ($('#package_content').is(':hidden')) {
        $('#package_content').css('display', 'block');
        $('#pkg-detail-colSpan').html('-');
    } else {
        $('#package_content').css('display', 'none');
        $('#pkg-detail-colSpan').html('+');

    }
};

var showFileName = function(name) {
    var pos1 = name.lastIndexOf('/');
    var pos2 = name.lastIndexOf('\\');
    var pos = Math.max(pos1, pos2)
    if (pos < 0)
        $('#pkg-file-name').val(name)
    else
        $('#pkg-file-name').val(name.substring(pos + 1))
};


var checkSize = function() {
    try{
    var maxFileSize = 50;  <!-- 文件大小100M -->
       var file = document.getElementById("pkgFile").value;
       var getAXO = new ActiveXObject("Scripting.FileSystemObject");
       var getF = getAXO.GetFile(file);
        if(getF.size>maxFileSize*1024*1024)  {
            alert("文件太大！");
            return ;
        }
    }catch(e){
}
    install();

};